package com.wushi.lenovo.asia5b.app_push;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.mvp.base.BaseApplication;
import com.mvp.dagger.modules.NetworkModule;
import com.mvp.utils.SharedPreferencesUtils;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_config.MalayAsiaApplication;
import com.wushi.lenovo.asia5b.app_config.UserRepository;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.PushBean;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_ui.inbox.activity.InboxActivity;
import com.wushi.lenovo.asia5b.app_ui.other.activity.MainActivity;
import com.wushi.lenovo.asia5b.app_utils.DeviceIdUtil;
import com.wushi.lenovo.asia5b.app_utils.helper.UriHelper;

import io.reactivex.schedulers.Schedulers;

/**
 * todo 目前还不清楚firebase和友盟是同时注册,还是只注册一个
 */
public class PushHelper {
    public static String registrationId = "";
    public static String registrationType = "";// 0 = firebase, 1 = umeng,9=自定义设备id
    public static String app_register_error_msg = "";
    private static final String REGISTRATION_NAME = "REGISTRATION_NAME";


    public static String getRegistrationId() {
        if (TextUtils.isEmpty(registrationId)) {
            registrationId = (String) SharedPreferencesUtils.getData(REGISTRATION_NAME, "");
        }
        if (TextUtils.isEmpty(registrationId)) {
            registrationId = DeviceIdUtil.getDeviceId(BaseApplication.sContext);
            registrationType = "9";
        }
        return registrationId;
    }

    /**
     * 测试用代码
     *
     * @param registrationId
     */
    public static void setRegistrationIdAndType(String registrationId, String registrationType) {
        PushHelper.registrationId = registrationId;
        PushHelper.registrationType = registrationType;
        SharedPreferencesUtils.putData(REGISTRATION_NAME, registrationId);
        //重新更新推送资料
        PushHelper.updateDeviceToken();
    }

    /**
     * 解析通知消息
     *
     * @param extra
     */
    public static void channelData(PushBean extra) {
        Activity endActivity = MalayAsiaApplication.getEndActivity();
        if (endActivity == null) {
            return;
        }
        if (extra == null) {
            return;
        }
        if (!TextUtils.isEmpty(extra.getUrl())) {
            UriHelper.parseUri(extra.getUrl(), endActivity);
        } else {
            String msgid = extra.getMsgid();
            if (UserRepository.isLogin()) {
                if (TextUtils.equals(msgid, "-1")) {
                    endActivity.startActivity(new Intent(endActivity, InboxActivity.class));
                } else {
                    endActivity.startActivity(new Intent(endActivity, MainActivity.class));
                }
            } else {
                endActivity.startActivity(new Intent(endActivity, MainActivity.class));
            }
        }
        MyFirebaseMessagingService.bean = null;
    }

    public static void updateDeviceToken() {
        if (!UserRepository.isLogin()) {
            return;
        }
        ApiService apiService = NetworkModule.getRetrofit().create(ApiService.class);
        MapHelper mapHelper = MapHelper.newHashMap();
        mapHelper.put("member_id", UserInfo.getMemberId());
        mapHelper.put("app_register_id", PushHelper.getRegistrationId());
        mapHelper.put("app_os", "1");
        mapHelper.put("app_notification_type", PushHelper.registrationType);
        mapHelper.put("app_register_error_msg", PushHelper.app_register_error_msg);
        apiService.updateDeviceToken(mapHelper)
                .subscribeOn(Schedulers.io())
                .subscribe(resultModel -> {
                }, throwable -> {
                    throwable.printStackTrace();
                });
    }
}
